package com.fancy.mobileschool.ui;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fancy.mobileschool.MyConfiguration;
import com.fancy.mobileschool.R;
import com.fancy.mobileschool.entity.Friend;
import com.fancy.mobileschool.service.ChatFriendsService;
import com.fancy.mobileschool.task.TaskExecutor;
import com.fancy.mobileschool.ui.adapt.ChatFriendsAdapt;
import com.fancy.mobileschool.ui.widget.DividerItemDecoration;

import java.util.List;

public class ChatFriendsListActivity extends AppCompatActivity implements View.OnClickListener{

    private RecyclerView recyclerView;

    private ChatFriendsAdapt adapt;

    private FloatingActionButton fab;

    private ChatFriendsService chatFriendsService = new ChatFriendsService();

    private TextView textView;

    private ImageView back;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_friends);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            actionBar.setCustomView(R.layout.custom_action_bar);
        }

        fab = (FloatingActionButton) findViewById(R.id.fab);
        recyclerView = (RecyclerView) findViewById(R.id.chat_friends_recycle_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
        textView = (TextView) findViewById(R.id.textView);
        assert textView != null;
        textView.setText("好友");
        back = (ImageView) findViewById(R.id.back);
        assert back != null;
        back.setOnClickListener(this);
        fab.setOnClickListener(this);


    }

    @Override
    protected void onStart() {
        super.onStart();
        TaskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                List<Friend> friends;
                friends = chatFriendsService.getMyFriends(MyConfiguration.myID);
                if (friends != null) {
                    adapt = new ChatFriendsAdapt(ChatFriendsListActivity.this, friends);
                    handler.sendEmptyMessage(1);
                }
            }
        });
    }

    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            recyclerView.setAdapter(adapt);
            adapt.notifyDataSetChanged();
        }
    };

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.back) {
            finish();
        }else if (view.getId() == R.id.fab) {
            Intent intent = new Intent(ChatFriendsListActivity.this, AddFriendActivity.class);
            startActivity(intent);
        }
    }
}
